package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-19 16:22
 */
public class GetMessageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();//
        GuestbookService service = new GuestbookServiceImpl();
        List<Object> list = service.showAll();
        //要将list集合在服务器端保存起来(后期就可以在页面中获取到list集合)
        req.setAttribute("list",list);
        //将资源转发到list.jsp中
        req.getRequestDispatcher("/list.jsp").forward(req,resp);
        //重定向
       //resp.sendRedirect(req.getContextPath()+"/list.jsp");

//        out.println("<html>");
//        out.println("<head>");
//        out.println("<title>test</title>");
//        out.println("</head>");
//        out.println("<body>");
//        out.println("<table border='1px' >");
//        out.println("<tr>");
//        out.println("<th>id</th>");
//        out.println("<th>name</th>");
//        out.println("<th>phone</th>");
//        out.println("<th>email</th>");
//        out.println("<th>title</th>");
//        out.println("<th>content</th>");
//        out.println("<th>createdtime</th>");
//        out.println("</tr>");
//
//
//
//        for (int i = 0; i < list.size(); i++) {
//            Guestbook gb = (Guestbook) list.get(i);
////            out.println(gb.toString());
//            out.println("<tr>");
//            out.println("<td>"+gb.getId()+"</td>");
//            out.println("<td>"+gb.getName()+"</td>");
//            out.println("<td>"+gb.getPhone()+"</td>");
//            out.println("<td>"+gb.getEmail()+"</td>");
//            out.println("<td>"+gb.getTitle()+"</td>");
//            out.println("<td>"+gb.getContent()+"</td>");
//            out.println("<td>"+gb.getCreatedtime()+"</td>");
//            out.println("</tr>");
//
//        }
//        out.println("</table>");
//        out.println("</body>");
//        out.println("</html");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
//        resp.setContentType("text/html;charset=utf-8");
//        req.setCharacterEncoding("utf-8");
//        PrintWriter out = resp.getWriter();//
//        GuestbookService service = new GuestbookServiceImpl();
//        List<Object> list = service.showAll();
//        out.println("<html>");
//        out.println("<head>");
//        out.println("<title>test</title>");
//        out.println("</head>");
//        out.println("<body>");
//        out.println("<table border='1px' >");
//        out.println("<tr>");
//        out.println("<th>id</th>");
//        out.println("<th>name</th>");
//        out.println("<th>phone</th>");
//        out.println("<th>email</th>");
//        out.println("<th>title</th>");
//        out.println("<th>content</th>");
//        out.println("<th>createdtime</th>");
//        out.println("</tr>");
//
//
//
//        for (int i = 0; i < list.size(); i++) {
//            Guestbook gb = (Guestbook) list.get(i);
////            out.println(gb.toString());
//            out.println("<tr>");
//            out.println("<td>"+gb.getId()+"</td>");
//            out.println("<td>"+gb.getName()+"</td>");
//            out.println("<td>"+gb.getPhone()+"</td>");
//            out.println("<td>"+gb.getEmail()+"</td>");
//            out.println("<td>"+gb.getTitle()+"</td>");
//            out.println("<td>"+gb.getContent()+"</td>");
//            out.println("<td>"+gb.getCreatedtime()+"</td>");
//            out.println("</tr>");
//
//        }
//        out.println("</table>");
//        out.println("</body>");
//        out.println("</html");
    }
}
